package com.vs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.vs.common.DuplicateTypeException;
import com.vs.model.LaborTypeForm;

public class LaborTypeOperation {


	private String insertString = "insert into labor_type(labor_type_value,added_by) values(?,?)";
	private String deleteString = "delete from labor_type where labor_type_id=?";
	private String getString = "select labor_type_id,labor_type_value from labor_type";

	public boolean deleteLaborType(Integer laborTypeId) {
		Connection conn = null;
		boolean success = false;
		try {
			conn = new DBConnection().getConnection();
			if (conn == null) {
				System.out.println("conn not found");
			} else {
				// System.out.println("conn found");
			}
			PreparedStatement ps = conn.prepareStatement(deleteString);
			ps.setInt(1, laborTypeId);
			ps.execute();
			ps.close();
			conn.commit();
			success = true;

		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return success;

	}

	
	public boolean addLaborType(LaborTypeForm laborTypeForm) throws DuplicateTypeException{
		Connection conn = null;
		boolean success = false;
		try {
			conn = new DBConnection().getConnection();
			if (conn == null) {
				System.out.println("conn not found");
			} else {
				//System.out.println("conn found");
			}
			PreparedStatement ps = conn
					.prepareStatement(insertString);
			ps.setString(1, laborTypeForm.getLaborType());
			ps.setString(2, laborTypeForm.getAddedBy());
			ps.execute();
			ps.close();
			conn.commit();
			success = true;

		} 
		catch (SQLException e) {
			System.out.println("SQLException " +e.getMessage());
			throw new DuplicateTypeException(e.getMessage());
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println("Exception " +e.getMessage());
			success = false;
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return success;
	}

	public List<LaborTypeForm> getLaborTypeLOV() {
		Connection conn = null;
		List<LaborTypeForm> laborTypeList = new ArrayList<LaborTypeForm>();

		try {
			conn = new DBConnection().getConnection();
			if (conn == null) {
				System.out.println("conn not found");
			} else {
				//System.out.println("conn found");
			}
			PreparedStatement ps = conn
					.prepareStatement(getString);
			// psExpenseTypeInsert.setString(1,
			// expenseTypeForm.getExpenseType());
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				LaborTypeForm laborType = new LaborTypeForm();
				laborType.setLaborTypeId(rs.getInt(1));
				laborType.setLaborType(rs.getString(2));
				laborTypeList.add(laborType);
			}
			ps.close();
			conn.commit();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return laborTypeList;
	}

}
